package org.jtb.quickr;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

class Get extends HttpVerb {
    static Logger logger = Logger.getLogger(Get.class.getName());
    
    public Get(URL url) {
        super(url);
    }    
    
    public InputStream execute() throws QuickrException {
        HttpClient httpClient = getHttpClient();
        HttpMethod getMethod = new GetMethod(getUrl().toString());
        
        try {
            int status = httpClient.executeMethod(getMethod);
            
            if (status != 200) {
                throw new QuickrException("call failed, status: " + status);
            }
            
            if (logger.isDebugEnabled()) {
                String response = getMethod.getResponseBodyAsString();
                logger.debug("rest response:\n" + response);
            }
            
            return getMethod.getResponseBodyAsStream();
        } catch (IOException ioe) {
            throw new QuickrException("could not call URL=" + getUrl(), ioe);
        }
    }    
}
